package data;

import static org.junit.Assert.assertTrue;

import java.util.Iterator;
import java.util.List;

import org.hibernate.*;
import org.hibernate.cfg.*;
import org.hibernate.criterion.*;
import org.hibernate.service.*;
import org.hibernate.Criteria;
import org.junit.BeforeClass;
import org.hibernate.SessionFactory;

import businessobjects.*;

public class UserDal implements IUserDal{
	
	static SessionFactory sessionFactory;
	static ServiceRegistry serviceRegistry;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		
		Configuration  conf = new Configuration().configure();
		serviceRegistry = new ServiceRegistryBuilder().applySettings(
				conf.getProperties()).buildServiceRegistry();
		sessionFactory = conf.buildSessionFactory(serviceRegistry);
	}
	

	@Override
	public User getUser(String username) {
		Session session = sessionFactory.openSession();
		Criteria cr = session.createCriteria(User.class);
		cr.add(Restrictions.like("username", username));
		List results = cr.list();
		session.close();
		return (User) results.get(1);
	}

	@Override
	public boolean insertUser(User user) {
		boolean effort = false;
	/*Session check = sessionFactory.openSession();
	Criteria cr = check.createCriteria(User.class);
	cr.add(Restrictions.like("username", user.getUsername()));
		List results = cr.list();
		
		for(int i = results.size(); i > 0; i++){
			if(results.get(i) == user.getUsername()){
				check.close();
				return false;
			}
		}
		
		Session insert = sessionFactory.openSession();
	  Transaction tx = null;
	  try{
		  tx = insert.beginTransaction();
	      User newUser = new User(user.getUsername(), user.getPasswort());
	      insert.save(newUser);
	      tx.commit();
	  }catch (HibernateException e) {
		  if (tx!=null) tx.rollback();
		  e.printStackTrace();
		  return false;
	  }finally {
		  insert.close();
	  }
	  return true;*/
	  
	  Session session = sessionFactory.openSession();
	  Transaction trans = session.beginTransaction();
	  User newUser = new User();
	  newUser.setPasswort(user.getPasswort());
	  newUser.setUsername(user.getUsername());
	  session.save(newUser);
	  trans.commit();
	  session.close();
	  if(newUser.getId() > 0) effort = true;
	  return effort;
	}
	
	public boolean login(User user){		
		boolean logedin = false;	
		
		/*try {
			//DB Connect
			Session session = sessionFactory.openSession();
			Criteria cr = session.createCriteria(User.class);
			Criterion uname = Restrictions.like("username", user.getUsername());
			Criterion passwd = Restrictions.like("password", user.getPasswort());
			LogicalExpression andExp = Restrictions.and(uname, passwd);
			cr.add( andExp );
			List results = cr.list();
			session.close();
			if(results.size() > 0){
				logedin = true;
			}
			return logedin;
		}
		catch(Exception hibernateException){
			System.out.println("Exception: " +hibernateException);
			System.out.println(user.getUsername() + " " + user.getPasswort());
			return logedin;
		}*/
		
		/*Session session = factory.openSession();
	      Transaction tx = null;
	      try{
	         tx = session.beginTransaction();
	         Criteria cr = session.createCriteria(User.class);
	         // Add restriction.
				Criterion uname = Restrictions.like("username", user.getUsername());
				Criterion passwd = Restrictions.like("password", user.getPasswort());
				LogicalExpression andExp = Restrictions.and(uname, passwd);
				cr.add( andExp );
	         /*for (Iterator iterator = employees.iterator(); iterator.hasNext();){
	             User DBuser = (User) iterator.next(); 
	             System.out.print("First Name: " + DBuser.getUsername()); 
	             System.out.print("  Last Name: " + DBuser.getPasswort());
	         }
	         tx.commit();
	         List results = cr.list();
	         if(results.size() > 0){
	        	 logedin = true;
	         }
	      }catch (HibernateException e) {
	         if (tx!=null) tx.rollback();
	         e.printStackTrace(); 
	      }finally {
	         session.close();
	      }
		
		return logedin;*/
		Session session = sessionFactory.openSession();
		
	      Transaction tx = null;
	      try{
	         tx = session.beginTransaction();
	         Criteria cr = session.createCriteria(User.class);
	         // Add restriction.
				Criterion uname = Restrictions.like("username", user.getUsername());
				Criterion passwd = Restrictions.like("password", user.getPasswort());
				LogicalExpression andExp = Restrictions.and(uname, passwd);
				cr.add( andExp );
	         tx.commit();
	         List results = cr.list();
	         if(results.size() > 0){
	        	 logedin = true;
	         }
	      }catch (HibernateException e) {
	         if (tx!=null) tx.rollback();
	         e.printStackTrace(); 
	      }finally {
	         session.close();
	      }
		  return logedin;
	}


}
